home *** CD-ROM | disk | FTP | other *** search
- #ifdef RCSID
- static char *RCSid =
- "$Header: examp10.pc.d,v 1.2.720.1 95/02/21 17:50:05 xxxxxxxx: Needtomrg_7_2 $ ";
- #endif /* RCSID */
-
- /* Copyright (c) 1991 by Oracle Corporation */
- /*
- NAME
- examp10.pc - <one-line expansion of the name>
- DESCRIPTION
- <short description of component this file declares/defines>
- PUBLIC FUNCTION(S)
- <list of external functions declared/defined - with one-line descriptions>
- PRIVATE FUNCTION(S)
- <list of static functions defined in .c file - with one-line descriptions>
- RETURNS
- <function return values, for .c file with single function>
- NOTES
- <other useful comments, qualifications, etc.>
- MODIFIED (MM/DD/YY)
- xxxxxxxx 02/13/95 - update
- xxxxxxxx 05/12/92 - Creation
- */
- /************************************************************************
- * *
- * EMBEDDED PL/SQL DEMO *
- * *
- * This program prompts for an account number and a debit or credit *
- * amount. If the account number is valid, a credit always succeeds, *
- * but a debit succeeds only if there are sufficient funds. *
- * *
- * Copyright (c) 1989,1992 by Oracle Corporation *
- ************************************************************************/
-
- #include <stdio.h>
-
- EXEC SQL BEGIN DECLARE SECTION;
- int acct, amount;
- VARCHAR tran_type[10];
- VARCHAR status[65];
- VARCHAR uid[20];
- VARCHAR pwd[20];
- EXEC SQL END DECLARE SECTION;
-
- EXEC SQL INCLUDE SQLCA;
-
- void sqlerror();
- main()
- {
- /* Set up userid and password */
- strcpy (uid.arr,"scott");
- uid.len=strlen(uid.arr);
- strcpy (pwd.arr,"tiger");
- pwd.len=strlen(pwd.arr);
-
- printf("\n\n\tEmbedded PL/SQL Demo\n\n");
- printf("Trying to connect...");
- /* Check for SQL errors */
- EXEC SQL WHENEVER SQLERROR DO sqlerror();
- /* Connect to Oracle */
- EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
- printf(" connected.\n");
-
- for (;;) /* Loop indefinitely */
- {
- printf("\n\n** Account number? (-1 to quit)");
- scanf("%d", &acct);
- if (acct == -1) /* Disconnect from Oracle and */
- { /* exit program if acct is -1 */
- EXEC SQL COMMIT WORK RELEASE;
- exit(0);
- }
- printf("\n Transaction type? (C)redit or (D)ebit ");
- scanf("%s", &tran_type.arr);
- tran_type.len = 1; /* Only want first character */
- printf("\n Transaction amount? (in whole dollars) ");
- scanf("%d", &amount);
-
- /* ----- Begin PL/SQL block ----- */
- EXEC SQL EXECUTE
- DECLARE
- old_bal NUMBER(11,2);
- no_account EXCEPTION;
- BEGIN
- :tran_type := UPPER(:tran_type);
- IF :tran_type = 'C' THEN -- credit the account
- UPDATE accounts SET bal = bal + :amount
- WHERE account_id = :acct;
- IF SQL%ROWCOUNT = 0 THEN -- no rows affected
- RAISE no_account;
- ELSE
- :status := 'Credit complete.';
- END IF;
- ELSIF :tran_type = 'D' THEN -- debit the account
- SELECT bal INTO old_bal FROM accounts
- WHERE account_id = :acct;
- IF old_bal >= :amount THEN -- has sufficient funds
- UPDATE accounts SET bal = bal - :amount
- WHERE account_id = :acct;
- :status := 'Debit applied';
- ELSE
- :status := 'Insufficient funds';
- END IF;
- ELSE
- :status := :tran_type || ' is an illegal transaction';
- END IF;
- COMMIT;
- EXCEPTION
- WHEN NO_DATA_FOUND OR no_account THEN
- :status := 'Nonexistent account';
- WHEN OTHERS THEN
- :status := 'Error: ' || SQLERRM(SQLCODE);
- END;
- END-EXEC;
- /* ----- End the PL/SQL block ----- */
-
- status.arr[status.len] = '\0'; /* null-terminate string */
- printf("\n\n Status: %s", status.arr);
- } /* End of loop */
- }
-
- void sqlerror()
- {
- /* Avoid infinite loop if rollback causes an error */
- EXEC SQL WHENEVER SQLERROR CONTINUE;
- printf("\nOracle error detected:\n");
- /* Print error message and disconnect from Oracle */
- printf("\n%.70s\n", sqlca.sqlerrm.sqlerrmc);
- EXEC SQL ROLLBACK WORK RELEASE;
- exit(1);
- }
-